<?php
class PartnerModel extends Model {
		/* 对应的数据表（不包含表明前缀） */
		protected	$tableName	=	'partner';
		
		
		/*
		*	查询所有商户信息
		`open`               enum('Y','N') not null default 'Y' comment '是否开放（保留），枚举型（Y,N）',
   `display`            enum('Y','N') not null default 'Y' comment '是否展示（保留）',
   `enable`             enum('Y','N') not null default 'Y' comment '是否可用',
		*/
		public function selectAllByPage($ary,$limited=20){
				if(is_string($ary)){
						if($ary)$ary = $ary." and open='Y' and display='Y' and is_delete='N' ";
						else $ary = " open='Y' and display='Y' and is_delete='N' ";
				}else{
					$ary['open']		=	'Y';
					$ary['display']	=	'Y';
					//$ary['enable']	=	'Y';
					$ary['is_delete']	=	'N';
				}
				//$pagePartners = $this->where($ary)->order(' parent_id asc,id desc ')->findPage($limited);
				$pagePartners = $this->kPageQuery($ary,' parent_id asc,id desc ',$limited);
				return $pagePartners;
		}
		
		/**
		*	查询所有一级商户信息。即parent_id = null 或 0 的商户。
		**/
		public function selectMainPartners($ary,$limited=20){
				if(is_string($ary)){
						if($ary)$ary = $ary." and parent_id=0 and open='Y' and display='Y' and is_delete='N' ";
						else $ary = " parent_id=0 and open='Y' and display='Y' and is_delete='N' ";
				}else{
					$ary['parent_id']		=	0;
					$ary['open']		=	'Y';
					$ary['display']	=	'Y';
					//$ary['enable']	=	'Y';
					$ary['is_delete']	=	'N';
				}
				//dump($ary);
				$listpartners = $this->where($ary)->order(' parent_id asc,id desc ')->findPage($limited);
				return $listpartners;
		
		}
		
		public function selectById($partnerId){
				$ary['id']		=	$partnerId;
				//$ary['open']		=	'Y';
				//$ary['display']	=	'Y';
				//$ary['enable']	=	'Y';
				$ary['is_delete']	=	'N';
				$partner = $this->where($ary)->find();
				return $partner;
		}
		
		/**
		 * 判断是否为总店
		 * @param unknown_type $partnerId
		 */
		public function isRootPartner($pid){
			$partner = $this->selectById($pid);
			//dump();
			if(empty($partner))return false;
			if($partner['parent_id']==0)return true;
			return false;
		}
		
		/**
		 * 判断pid是否为parentid的总店或总店的分店。
		 */
		public function isBelongPartner($pid,$parentid){
			Log::write('PartnerModel.isBelongPartner('.$pid.','.$parentid.')',Log::DEBUG,Log::FILE);
			
			$partner = $this->selectById($pid);
			Log::write('PartnerModel.isBelongPartner('.$pid.','.$parentid.')查询的商户pid->'.parseAry2Str($partner),Log::DEBUG,Log::FILE);
			if(empty($partner))return false;
			if( ( $partner['parent_id']==0 && $partner['id']==$parentid ) || $partner['parent_id']==$parentid )return true;
			return false;
		}
		
		/**
		 * 获得总店id
		 */
		public function getRootPid($pid){
			$partner = $this->selectById($pid);
			if(empty($partner))return -1;
			if( $partner['parent_id']==0 ){
				return $partner['id'];
			}else{
				return $partner['parent_id'];
			}
		}
		/**
		 * 查询商户信息
		 * @param unknown_type $map		包含参数pid-商户id  ppid-上级商户ID  psort商户类型    porppid-表示查询商户及其下级商户
		 * @param unknown_type $field
		 * @param unknown_type $order
		 * @param unknown_type $limit
		 */
		public function selectPartnerInfo($map,$field='*',$order=null,$limit=null,$ifpage=false){
			if(!empty($map['pid']))$ary ['id'] = $map['pid'];
			if(!empty($map['ppid']))$ary ['parent_id'] = $map['ppid'];
			if(!empty($map['porppid']))$ary['id'] = array('EXP',"=".$map['porppid']." OR `parent_id`=".$map['porppid']);
			if(!empty($map['psort']))$ary ['sort_id'] = $map['psort'];
			if(!empty($map['title']))$ary ['title'] = $map['title'];
			if(!empty($map['noroot']))$ary ['parent_id'] = array('EXP',">0");//表示不查询根数据
			$ary ['is_delete'] = 'N';
			
			if(!$ifpage){
				$data = $this->where( $ary )->field($field)->order($order)->limit($limit)->findAll();
			}else{
				$data = $this->kPageQuery($ary,$order,$limit,$field);
				
			}
			//dump($data);
			//dump($this->getLastSql());
			return $data;
		}
		
		public function deleteByIds($ids){
				$map['id']	= array('in', $ids);
				$ary['is_delete']	=	'Y';
				$no = $this->where($map)->save($ary);
				return $no;
		}
		
		/**
		 * 更新商户验证商品总数
		 * @param unknown_type $pid
		 * @param unknown_type $checkednumber
		 */
		public function refreshCheckedNo($pid,$checkednumber){
			$partner = $this->where('id='.$pid)->find();
			if(empty($partner))return false;
			$map['checked_number']=intval($partner['checked_number'])+intval($checkednumber);
			$this->where('id='.$pid)->save($map);
			return $map['checked_number'];
			
		}
		
}
?>